#include "QAbstractMuscle.h"

QAbstractMuscle::QAbstractMuscle(void)
{
    tempMemPot = 0;
    type = 0;
    this->setMemPot(0);
    this->setID(0);
    this->setDefL(100);
    this->setK(10);
    this->calculateCenterPos();
}

int QAbstractMuscle::getID(void)
{
    return id;
}
int QAbstractMuscle::getType(void)
{
    return type;
}
double QAbstractMuscle::getDefL(void)
{
    return defL;
}
double QAbstractMuscle::getK(void)
{
    return k;
}

void QAbstractMuscle::calculateCenterPos(void)
{
    centerPos = QPointF(0,0);

    for (int i=0; i<nList.length(); ++i)
    {
        centerPos += QPointF(nList[i]->getPos().x(), nList[i]->getPos().y());
    }

    centerPos = QPointF(centerPos.x()/nList.length(), centerPos.y()/nList.length());
}
QPointF QAbstractMuscle::getCenterPos(void)
{
    return centerPos;
}

QList<QNode*> QAbstractMuscle::getNodeList(void)
{
    return nList;
}

void QAbstractMuscle::setID(int _id)
{
    if (_id>=0) id = _id;
    emit idChanged(id);
}
void QAbstractMuscle::setDefL(qreal _defL)
{
    l = _defL;
    defL = _defL;
    emit defLChanged(defL);
}
void QAbstractMuscle::setK(double _k)
{
    k = _k;
    emit kChanged(k);
}

void QAbstractMuscle::setMemPot(double _memPot)
{
    memPot = _memPot;
    emit memPotChanged(memPot);
}
void QAbstractMuscle::addMemPot(double _memPot)
{
    tempMemPot += _memPot;
}
void QAbstractMuscle::rotMemPot(void)
{
    memPot = tempMemPot;
    tempMemPot = 0;
    emit memPotChanged(memPot);
}

void QAbstractMuscle::addNode(QNode *node)
{
    if (nList.length() < 2) nList << node;
    this->calculateCenterPos();
}
void QAbstractMuscle::sendForce(void)
{
    this->calculateForce();
    for (int i=0; i<nList.length(); ++i)
    {
        nList[i]->addXForce(f*(nList[i]->getPos().x()-nList[!i]->getPos().x())/l);
        nList[i]->addYForce(f*(nList[i]->getPos().y()-nList[!i]->getPos().y())/l);
    }
}
void QAbstractMuscle::emitEverything(void)
{
    emit idChanged(id);
    emit defLChanged(defL);
    emit kChanged(k);
}

